home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / 7edit / source / svaecoercions.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.6 KB  |  144 lines

  1. /*
  2.     File:        SVAECoercions.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Original version by Jon Lansdell and Nigel Humphreys.
  7.                 3.1 updates by Greg Sutton.
  8.  
  9.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/20/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24.  
  25. #include "SVAECoercions.h"
  26.  
  27. #include "SVEditAEUtils.h"
  28. #include "SVAETextUtils.h"
  29.  
  30. #include "SVAEAccessors.h"
  31. #include "SVAESelect.h"
  32. #include "SVToken.h"
  33.  
  34.  
  35. #pragma segment AppleEvent
  36.  
  37. // Install coercion handlers that coerce a descriptor from one type to another.
  38.  
  39. OSErr    InstallCoercions(void)
  40. {
  41.     OSErr    err;
  42.  
  43.       err = AEInstallCoercionHandler(typeObjectSpecifier, typeMyAppl,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  44.     err = AEInstallCoercionHandler(typeObjectSpecifier, typeMyWndw,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  45.       err = AEInstallCoercionHandler(typeObjectSpecifier, typeMyText,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  46.       err = AEInstallCoercionHandler(typeObjectSpecifier, typeMyTextProp,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  47.       err = AEInstallCoercionHandler(typeObjectSpecifier, typeMyWindowProp,(AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  48.       err = AEInstallCoercionHandler(typeObjectSpecifier, typeMyApplProp,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  49.  
  50.       err = AEInstallCoercionHandler(typeMyWndw, typeMyText,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceWindowToText), 0, true, false);
  51.       err = AEInstallCoercionHandler(typeMyWindowProp, typeMyText,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceWindowPropertyToText), 0, true, false);
  52.  
  53.     return(err);
  54. }
  55.  
  56.  
  57. // Takes an object specifier that it resolves using AEResolve
  58. // then tries to coerce this result into the type specified by toType.
  59.  
  60. pascal OSErr    CoerceObjToAnything(const AEDesc    *theAEDesc,
  61.                                     DescType        toType,
  62.                                     long            handlerRefCon,
  63.                                     AEDesc            *result)
  64. {
  65. #pragma unused (handlerRefCon)
  66.  
  67.     AEDesc     objDesc = {typeNull, NULL};
  68.     OSErr      err;    
  69.     
  70.     if (theAEDesc->descriptorType != typeObjectSpecifier)
  71.         return(errAEWrongDataType);
  72.     
  73.         // resolve the object specifier
  74.     err = AEResolve(theAEDesc, kAEIDoMinimum, &objDesc);
  75.     if (noErr != err) goto done;
  76.         
  77.         // hopefully it's the right type by now, but we'll give it a nudge
  78.     err = AECoerceDesc(&objDesc, toType, result);
  79.  
  80. done:
  81.     if (objDesc.dataHandle)
  82.         AEDisposeDesc(&objDesc);
  83.             
  84.     return(err);
  85. }    // CoerceObjToAnything
  86.  
  87.  
  88. // A window is effectively a text item that contains all the text in the window
  89. // so allow coercion of windows to text.
  90.  
  91. pascal OSErr    CoerceWindowToText(AEDesc    *theAEDesc,
  92.                                     DescType        toType,
  93.                                     long            handlerRefCon,
  94.                                     AEDesc            *result)
  95. {
  96. #pragma unused (toType, handlerRefCon)
  97.     
  98.     return(TextDescFromWindowDesc(theAEDesc, result));
  99. }
  100.  
  101. // Some window properties are effectively text so allow a coercion
  102. // e.g. set selection of window 1 to "Something"
  103.  
  104. pascal OSErr    CoerceWindowPropertyToText(AEDesc        *theAEDesc,
  105.                                             DescType    toType,
  106.                                             long        handlerRefCon,
  107.                                             AEDesc        *result)
  108. {
  109. #pragma unused (toType, handlerRefCon)
  110.     
  111.     WindowPropToken        aWindowPropToken;
  112.     TextToken            aTextToken;
  113.     Size                actualSize;
  114.     short                ignore;
  115.     OSErr                err;
  116.  
  117.     GetRawDataFromDescriptor(theAEDesc, (Ptr)&aWindowPropToken,
  118.                                         sizeof(aWindowPropToken), &actualSize);
  119.     
  120.     switch (aWindowPropToken.tokenProperty)
  121.     {
  122.         case pText:
  123.         case pContents:
  124.             err = TextDescFromWindowToken(&(aWindowPropToken.tokenWindowToken), result);
  125.             break;
  126.  
  127.         case pSelection:
  128.             err = GetWindowSelection(aWindowPropToken.tokenWindowToken.tokenWindow,
  129.                                                                     &aTextToken, &ignore);
  130.             if (noErr != err) goto done;
  131.             
  132.             err = AECreateDesc(typeMyText, (Ptr)&aTextToken, sizeof(aTextToken), result);
  133.             break;
  134.             
  135.         default:            // Most properties don't make sense to be coerced from
  136.             err = errAECoercionFail;
  137.     }                        // e.g 'select insertion point before bounds of document 1'
  138.     
  139. done:    
  140.     return(err);
  141. }
  142.  
  143.  
  144.